<?php
include("_session.php");
if(count($_POST)>0)
{
	$order_no = $_POST['ORDNO'];
	$ordcamph = $_POST["ORDCAMP"];		
	$dist = $_POST["DIST"];
	$mslno = $_POST["MSLNO"];
	$chkdgt = $_POST["CHKDGT"];
	$upddate = $_POST["UPDDATE"];
	$updtime = $_POST["UPDTIME"];
	$delflag = "N";
	$dwnflag = "N";

	$rows = $_SESSION['ORDER_DETAIL'];
	$i=0;
	if (HeaderExists($conn,$order_no,$ordcamph,$dist,$mslno,$chkdgt,$upddate,$updtime))
	{
		$respond_message = "CONCURRENCE";
		$error_message = "CONCURRENCE! ".$order_no."/".$ordcamph."(".$dist."-".$mslno."-".$chkdgt.")";
	}
	else
	{
        mysql_query("BEGIN");        
		$sql = "UPDATE order_detail 
			SET DELFLAG = 'Y'  
			WHERE 
				ORDER_NO = ".$order_no." AND
				ORDCAMPH = ".$ordcamph." AND 
				DIST = '".$dist."' AND
				MSLNO = ".$mslno." AND 
				CHKDGT = ".$chkdgt." AND 				
				DWNFLAG = '".$dwnflag."' AND 
				DELFLAG = '".$delflag."'";
				//echo $sql;
    	$result = mysql_query($sql, $conn);
    	if (!$result)
    	{
    		$log = date("H:i:s")."-".$_SESSION["RACCOUNT_ID"];
    		error_log($log.": ".mysql_error($conn).PHP_EOL, 3, "D:/AppServ/www/tosorderbackup/log/err".date("Ymd").".log");
			mysql_query("ROLLBACK");	    		
    		$respond_message = "ERROR";
			$error_message = "ROLLBACK! D".$order_no."/".$ordcamph."(".$dist."-".$mslno."-".$chkdgt.")";
			//echo mysql_errno($conn) . ": " . mysql_error($conn). "\n";
		}
		else
		{	
			if ($result)
			{
				$sql = "UPDATE order_header 
				SET 
					DELFLAG = 'Y', 
					ACCOUNT_ID = '".$_SESSION["RACCOUNT_ID"]."',
					UPDDATE = '".date("Ymd")."',
					UPDTIME = '".date("His")."' 
				WHERE 
					ORDER_NO = ".$order_no." AND
					ORDCAMP = ".$ordcamph." AND 
					DIST = '".$dist."' AND
					MSLNO = ".$mslno." AND 
					CHKDGT = ".$chkdgt." AND 				
					DWNFLAG = '".$dwnflag."' AND 
					DELFLAG = '".$delflag."'";
				//echo $sql;
				$result = mysql_query($sql, $conn);
		    	if (!$result)
		    	{
    				$log = date("H:i:s")."-".$_SESSION["RACCOUNT_ID"];
    				error_log($log.": ".mysql_error($conn).PHP_EOL, 3, "D:/AppServ/www/tosorderbackup/log/err".date("Ymd").".log");		    		
					mysql_query("ROLLBACK");	    		
		    		$respond_message = "ERROR";
					$error_message = "ROLLBACK! H".$order_no."/".$ordcamph."(".$dist."-".$mslno."-".$chkdgt.")";
					//echo mysql_errno($conn) . ": " . mysql_error($conn). "\n";
				}
				else
				{
					mysql_query("COMMIT");
					$respond_message = "SUCCESSFULL";
				}
			}			
		}			
	}
	header("Location:resultdelete.php?respond_message=".$respond_message);	
}

function HeaderExists($conn,$order_no,$ordcamph,$dist,$mslno,$chkdgt,$upddate,$updtime)
{
	$sql = "SELECT count(*) as numrow 
	FROM order_header oh 
		WHERE 
		oh.DIST ='".$dist."'  
	AND oh.MSLNO = ".$mslno." 
	AND oh.CHKDGT = ".$chkdgt." 
	AND oh.ORDCAMP = ".$ordcamph." 
	AND oh.ORDER_NO = ".$order_no."
	AND oh.UPDDATE = '".$upddate."'  
	AND oh.UPDTIME = '".$updtime."'";
	//echo $sql;
	$raw = mysql_query($sql, $conn);
	$row = mysql_fetch_array($raw, MYSQL_ASSOC);
	if ($row["numrow"] > 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

include("_close.php");
?>